﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace ZHello.Utils
{
    public class DotNetEnvInstallUtil
    {
        public static void GetAllVersions(out List<string> versions)
        {
            versions = new List<string>();
            //HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\SKUs\\.NETFramework,Version=v4.6.2
            //计算机\HKEY_LOCAL_MACHINE\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost
            var path = "SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\SKUs";
            try
            {
                var reg = Registry.LocalMachine.OpenSubKey(path).GetSubKeyNames();
                versions.AddRange(reg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public const string Urlnet60_x64 = "https://download.visualstudio.microsoft.com/download/pr/7f3a766e-9516-4579-aaf2-2b150caa465c/d57665f880cdcce816b278a944092965/windowsdesktop-runtime-6.0.3-win-x64.exe";
        public const string Urlnet472 = "https://download.visualstudio.microsoft.com/download/pr/1f5af042-d0e4-4002-9c59-9ba66bcf15f6/089f837de42708daacaae7c04b7494db/ndp472-kb4054530-x86-x64-allos-enu.exe";
        public static string DownloadRuntime(string url, string file)
        {
            var urlx = new Uri(Assembly.GetExecutingAssembly().Location);
            var downPath = Path.Combine(Path.GetDirectoryName(urlx.LocalPath), "download");
            if (!Directory.Exists(downPath))
                Directory.CreateDirectory(downPath);
            var f = Path.Combine(downPath, file);
            if (File.Exists(f))
                File.Delete(f);
            try
            {
                var req = WebRequest.Create(url);
                var rep = req.GetResponse();
                if (rep.ContentType.ToLower().Length > 0)
                {
                    using (var reader = rep.GetResponseStream())
                    {
                        using (var writer = File.Create(f))
                        {
                            byte[] buff = new byte[1024 * 2];
                            int c = 0;
                            do
                            {
                                c = reader.Read(buff, 0, buff.Length);
                                if (c > 0)
                                    writer.Write(buff, 0, c);
                            } while (c > 0);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return f;
        }

        public static void InstallRuntime(string file)
        {
            if (!File.Exists(file))
                return;
            //https://sourceforge.net/projects/nsis/
            //https://nsis.sourceforge.io/Get_PDA_processor_type
            ///xxx.exe /q /norestart /ChainingPackage FullX64Bootstrapper
        }
    }
}
